package com.sgb.kjwl.view.ui.activity.self.order;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOrderEditBinding;
import com.sgb.kjwl.di.order.DaggerEditOrderComponent;
import com.sgb.kjwl.di.order.EditOrderModule;
import com.sgb.kjwl.model.entity.OrderUploadEntity;
import com.sgb.kjwl.model.entity.user.OrderServiceEntity;
import com.sgb.kjwl.view.ui.adapter.self.order.OrderServiceAdapter;
import com.sgb.kjwl.viewmodel.self.EditOrderModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 2019-9-16 10:00:15 服务维护页面
 */
public class EditOrderActivity extends AppActivity {

    @Inject
    EditOrderModel viewModel;
    private ActivityOrderEditBinding binding;
    private String id;
    private String type;
    private OrderServiceAdapter orderServiceAdapter;
    private boolean isEdit;
    private List<OrderServiceEntity> data;

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    public static void start(Context context, String id, String type) {
        Intent intent = new Intent();
        intent.setClass(context, EditOrderActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEditOrderComponent.builder().appComponent(appComponent).editOrderModule(new EditOrderModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(EditOrderActivity.this, R.layout.activity_order_edit);
        binding.include.ivBack.setOnClickListener(this);
        binding.tvSave.setOnClickListener(this);
    }

    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        if (type.equals("4") || type.equals("14")) {
            binding.include.titleView.setText("服务维护");
            binding.tvSave.setVisibility(View.VISIBLE);
            isEdit = true;
        } else {
            binding.include.titleView.setText("服务查看");
            binding.tvSave.setVisibility(View.INVISIBLE);
            isEdit = false;
        }
        viewModel.getChangeList(id);

        orderServiceAdapter = new OrderServiceAdapter(this, null, isEdit);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.listCategory.setLayoutManager(layoutManager);
        binding.listCategory.setAdapter(orderServiceAdapter);
        binding.listCategory.setLoadingMoreEnabled(false);
        binding.listCategory.setHasFixedSize(false);
        binding.listCategory.setNestedScrollingEnabled(false);
        binding.listCategory.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.getChangeList(id);
            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.tv_save:
                ArrayList<OrderUploadEntity> entities = new ArrayList<>();

                if (data.get(0).getIsTransport().equals("1") && data.get(0).getIsLoading().equals("1")) {
                    if (orderServiceAdapter.getTransportPrice().size() == data.size() && orderServiceAdapter.getLoadingPrice().size() == data.size()) {
                        for (int i = 0; i < data.size(); i++) {
                            OrderUploadEntity entity = new OrderUploadEntity();
                            entity.setCid(data.get(i).getCid());
                            entity.setName(data.get(i).getName());
                            entity.setOrderId(data.get(i).getOrderId());
                            entity.setTransportPrice(orderServiceAdapter.getTransportPrice().get(i + 1));
                            entity.setLoadingPrice(orderServiceAdapter.getLoadingPrice().get(i + 1));
                            entities.add(entity);
                        }
                    } else {
                        MToast.showToast(EditOrderActivity.this, "请补充完整服务费用信息再进行提交");
                    }
                }

                if (data.get(0).getIsLoading().equals("1") && !data.get(0).getIsTransport().equals("1")) {
                    if (orderServiceAdapter.getLoadingPrice().size() == data.size()) {
                        for (int i = 0; i < data.size(); i++) {
                            OrderUploadEntity entity = new OrderUploadEntity();
                            entity.setCid(data.get(i).getCid());
                            entity.setName(data.get(i).getName());
                            entity.setOrderId(data.get(i).getOrderId());
                            entity.setTransportPrice("0");
                            entity.setLoadingPrice(orderServiceAdapter.getLoadingPrice().get(i + 1));
                            entities.add(entity);
                        }
                    } else {
                        MToast.showToast(EditOrderActivity.this, "请补充完整服务费用信息再进行提交");
                    }
                }

                if (!data.get(0).getIsLoading().equals("1") && data.get(0).getIsTransport().equals("1")) {
                    if (orderServiceAdapter.getTransportPrice().size() == data.size()) {
                        for (int i = 0; i < data.size(); i++) {
                            OrderUploadEntity entity = new OrderUploadEntity();
                            entity.setCid(data.get(i).getCid());
                            entity.setName(data.get(i).getName());
                            entity.setOrderId(data.get(i).getOrderId());
                            entity.setTransportPrice(orderServiceAdapter.getTransportPrice().get(i + 1));
                            entity.setLoadingPrice("0");
                            entities.add(entity);
                        }
                    } else {
                        MToast.showToast(EditOrderActivity.this, "请补充完整服务费用信息再进行提交");
                    }
                }

                viewModel.saveChangeList(entities);
                break;
        }
    }

    public void showOrderList(List<OrderServiceEntity> data) {
        this.data = data;
        binding.listCategory.refreshComplete();
        orderServiceAdapter.setDatas(data);
    }
}
